package edu.hubu.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import net.moon.jpa.model.BaseEntity;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author moonlan
 * @date 2020/10/18 下午12:52
 */
@Entity
@Table(name = "course_hour")
//逻辑删除注解，删除sql变成了update
@SQLDelete(sql = "update course_hour set course_hour_is_deleted = true where course_hour_id = ?")
//where条件带上了逻辑删除条件
@Where(clause = "course_hour_is_deleted = false")
@ApiModel("学时")
@JsonIgnoreProperties(value={"hibernateLazyInitializer", "handler", "fieldHandler"})
public class CourseHour  extends BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeId")
    @GenericGenerator(name = "snowflakeId", strategy = "edu.hubu.commons.SnowflakeId",
                      parameters = {@org.hibernate.annotations.Parameter(name = "prefix", value = "COURSE-HOUR")})
    @Column(name = "course_hour_id")
    @ApiModelProperty(value = "class_hour的id", position = 1)
    private String courseHourId;

    @Column(name = "course_hour_time")
    @ApiModelProperty(value = "学时", position = 2)
    private Integer courseHour;

    @Column(name = "course_hour_is_forbidden")
    @ApiModelProperty(value = "学时是否被禁用", position = 3)
    private boolean courseHourIsForbidden = false;

    @Column(name = "course_hour_is_deleted")
    @ApiModelProperty(value = "学时是否被删除", position = 4)
    private boolean courseHourIsDeleted = false;

    @OneToMany(mappedBy = "courseHour", fetch = FetchType.LAZY)
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private List<TeacherCourse> teacherCourses = new ArrayList<>();

    @OneToMany(mappedBy = "courseHour", fetch = FetchType.LAZY)
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private List<StudentCourse> studentCourses = new ArrayList<>();

    public CourseHour(String courseHourId, Integer courseHour, boolean courseHourIsForbidden, boolean courseHourIsDeleted, List<TeacherCourse> teacherCourses, List<StudentCourse> studentCourses) {
        this.courseHourId = courseHourId;
        this.courseHour = courseHour;
        this.courseHourIsForbidden = courseHourIsForbidden;
        this.courseHourIsDeleted = courseHourIsDeleted;
        this.teacherCourses = teacherCourses;
        this.studentCourses = studentCourses;
    }

    public CourseHour() {
    }

    public static CourseHourBuilder builder() {
        return new CourseHourBuilder();
    }

    public String getCourseHourId() {
        return this.courseHourId;
    }

    public Integer getCourseHour() {
        return this.courseHour;
    }

    public boolean isCourseHourIsForbidden() {
        return this.courseHourIsForbidden;
    }

    public boolean isCourseHourIsDeleted() {
        return this.courseHourIsDeleted;
    }

    public List<TeacherCourse> getTeacherCourses() {
        return this.teacherCourses;
    }

    public List<StudentCourse> getStudentCourses() {
        return this.studentCourses;
    }

    public void setCourseHourId(String courseHourId) {
        this.courseHourId = courseHourId;
    }

    public void setCourseHour(Integer courseHour) {
        this.courseHour = courseHour;
    }

    public void setCourseHourIsForbidden(boolean courseHourIsForbidden) {
        this.courseHourIsForbidden = courseHourIsForbidden;
    }

    public void setCourseHourIsDeleted(boolean courseHourIsDeleted) {
        this.courseHourIsDeleted = courseHourIsDeleted;
    }

    public void setTeacherCourses(List<TeacherCourse> teacherCourses) {
        this.teacherCourses = teacherCourses;
    }

    public void setStudentCourses(List<StudentCourse> studentCourses) {
        this.studentCourses = studentCourses;
    }

    public static class CourseHourBuilder {
        private String courseHourId;
        private Integer courseHour;
        private boolean courseHourIsForbidden;
        private boolean courseHourIsDeleted;
        private List<TeacherCourse> teacherCourses;
        private List<StudentCourse> studentCourses;

        CourseHourBuilder() {
        }

        public CourseHourBuilder courseHourId(String courseHourId) {
            this.courseHourId = courseHourId;
            return this;
        }

        public CourseHourBuilder courseHour(Integer courseHour) {
            this.courseHour = courseHour;
            return this;
        }

        public CourseHourBuilder courseHourIsForbidden(boolean courseHourIsForbidden) {
            this.courseHourIsForbidden = courseHourIsForbidden;
            return this;
        }

        public CourseHourBuilder courseHourIsDeleted(boolean courseHourIsDeleted) {
            this.courseHourIsDeleted = courseHourIsDeleted;
            return this;
        }

        public CourseHourBuilder teacherCourses(List<TeacherCourse> teacherCourses) {
            this.teacherCourses = teacherCourses;
            return this;
        }

        public CourseHourBuilder studentCourses(List<StudentCourse> studentCourses) {
            this.studentCourses = studentCourses;
            return this;
        }

        public CourseHour build() {
            return new CourseHour(courseHourId, courseHour, courseHourIsForbidden, courseHourIsDeleted, teacherCourses, studentCourses);
        }

        public String toString() {
            return "CourseHour.CourseHourBuilder(courseHourId=" + this.courseHourId + ", courseHour=" + this.courseHour + ", courseHourIsForbidden=" + this.courseHourIsForbidden + ", courseHourIsDeleted=" + this.courseHourIsDeleted + ", teacherCourses=" + this.teacherCourses + ", studentCourses=" + this.studentCourses + ")";
        }
    }
}
